package cn.hrlly.tao2shou.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.CommoditiesAdapter;
import cn.hrlly.tao2shou.adapter.RequiresAdapter;
import cn.hrlly.tao2shou.domain.Commodities;
import cn.hrlly.tao2shou.domain.RequireList;
import cn.hrlly.tao2shou.domain.Requires;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.RequireListPresenter;
import cn.hrlly.tao2shou.presenter.RequireListPresenterImpl;
import cn.hrlly.tao2shou.ui.RefreshLayout;
import cn.hrlly.tao2shou.utils.NetUtil;
import cn.hrlly.tao2shou.view.activity.CommodityDetailsActivity;
import cn.hrlly.tao2shou.view.activity.RequireDetailsActivity;
import cn.hrlly.tao2shou.view.view_interface.RequireListView;

public class RequireFragment extends Fragment implements RequireListView {

    @Bind(R.id.lvRequires)
    ListView lvRequires;
    @Bind(R.id.refresh)
    RefreshLayout refresh;
    private static int page;
    private int pageCount;
    private String classification;
    private RequireListPresenter requireListPresenter;
    private RequiresAdapter adapter;
    private ArrayList<Requires> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_require, container, false);
        //绑定控件
        ButterKnife.bind(this, rootView);
        //初始化
        init();
        //判断是否有网，有连接后台，没有显示缓存
        if(NetUtil.ping(getActivity())){
            requireListPresenter.showRequireList();
        }else {
            requireListPresenter.showCacheRequireList();
        }
        refresh();
        clickListview();
        return rootView;
    }

    //初始化
    public void init(){
        page=0;
        pageCount=20;
        classification="综合";
        requireListPresenter=new RequireListPresenterImpl(this,getActivity());
        list=null;
    }

    //对listview的点击事件,跳转至详情页面
    public void clickListview(){
        lvRequires.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),RequireDetailsActivity.class);
                intent.putExtra(Config.key_require_id,list.get(position).getRequireId());
                startActivity(intent);
            }
        });
    }

    //下拉刷新和加载更多
    public void refresh(){
        //下拉刷新
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page=1;
                        list=null;
                        requireListPresenter.showRequireList();
                        refresh.setRefreshing(false);
                    }
                },1000);
            }
        });

        //加载更多
        refresh.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                refresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        requireListPresenter.showRequireList();
                        refresh.setLoading(false);
                    }
                },1000);
            }
        });
    }

    //以下是对fragment的操作
    @Override
    public void showListView(RequireList requireList) {
        if(list==null) {
            list = new ArrayList<Requires>();
            list.addAll(requireList.getRequires());
            adapter=new RequiresAdapter(list,getActivity());
            lvRequires.setAdapter(adapter);
        }else {
            //当下拉刷新或者更改sortAccording时page=1,list清空，刷新adapter,调用adapter.refresh
            //当加载更多时page++，list添加,刷新adapter,调用adapter.addMore
            list.addAll(requireList.getRequires());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public int getPageCount() {
        return pageCount;
    }

    @Override
    public String getClassification() {
        return classification;
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(getActivity(),errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(getActivity(),errorMessage,Toast.LENGTH_SHORT).show();
    }

}
